import "reflect-metadata";
import BeanFactory from "@framework/bean/BeanFactory";
import {DECORATOR_SUFFIX} from "@framework/decorator";
import BeanDefinition from "@framework/bean/BeanDefinition";
import KoaBootLogger from "@framework/web/KoaBootLogger";
import BeanEnum from "@framework/enum/BeanEnum";

/**
 * 产生Bean对象工厂
 */
class DefaultListableBeanFactory extends BeanFactory {

    /**
     * 日志
     * @private
     */
    private readonly logger: KoaBootLogger = KoaBootLogger.getInstance();

    /**
     * 构造函数
     */
    constructor() {
        super();
    }

    /**
     * 注册 BeanDefinition 类
     * @param object
     */
    public registerBeanDefinition(params: any): void {
        const key = DECORATOR_SUFFIX.generator(params.defaultModule.name, DECORATOR_SUFFIX.BEAN);
        const beanDefinition: BeanDefinition = Reflect.getOwnMetadata(key, params.defaultModule);
        if (beanDefinition) {
            beanDefinition.setDefaultModule(params.defaultModule);
            beanDefinition.setScope(BeanEnum.SINGLETON)
            beanDefinition.setPath(params.path)
            this.getBeanDefinitions().set(params.defaultModule.name, beanDefinition);
        }
    }

    /**
     * 获取Bean实例
     * @param beanName
     */
    public getBean(beanName: string): Object | undefined {
        const singleton = this.getSingleton();
        return singleton.get(beanName);
    }
}

export default DefaultListableBeanFactory;